package com.rokyinfo.pdm.controller.v1;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DMcuDevice;
import com.rokyinfo.pdm.service.IDMcuDeviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * <p>
 * 没有SN号的无SN号设备表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-22
 */
@Api(value = "/noSnDevices", tags = "NoSnDevices", description = "无SN号设备")
@Controller
@RequestMapping(BasicPath.V1 + "/noSnDevices")
public class DMcuDeviceController {
    @Autowired
    private IDMcuDeviceService mIDMcuDeviceService;

    @ApiOperation(value = "获取无SN号设备列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<DMcuDevice>> getDMcuDeviceList() throws Exception {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        EntityWrapper<DMcuDevice> wrapper = new EntityWrapper<DMcuDevice>();
        return ResponseEntity.ok(mIDMcuDeviceService.selectPage(new Page<DMcuDevice>(0, 2),wrapper));
    }

    @ApiOperation(value = "新建无SN号设备", notes = "根据DMcuDevice对象创建无SN号设备")
    @ApiImplicitParam(name = "DMcuDevice", value = "无SN号设备实体DMcuDevice", required = true, dataType = "DMcuDevice")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postDMcuDevice(@RequestBody DMcuDevice DMcuDevice) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDMcuDeviceService.insert(DMcuDevice)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取无SN号设备信息", notes = "根据url的id来获取无SN号设备信息")
    @ApiImplicitParam(name = "id", value = "无SN号设备ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<DMcuDevice> getDMcuDevice(@PathVariable Long id) {
        DMcuDevice mDMcuDevice = mIDMcuDeviceService.selectById(id);
        if (mDMcuDevice != null) {
            return ResponseEntity.ok(mDMcuDevice);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新无SN号设备信息", notes = "根据url的id来指定更新对象，并根据传过来的DMcuDevice信息来更新无SN号设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "无SN号设备ID", required = true, dataType = "Long", paramType = "path"),
            @ApiImplicitParam(name = "DMcuDevice", value = "无SN号设备实体DMcuDevice", required = true, dataType = "DMcuDevice")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseEntity putDMcuDevice(@PathVariable String id, @RequestBody DMcuDevice DMcuDevice) {
        DMcuDevice.setUdid(id);
        if (mIDMcuDeviceService.updateById(DMcuDevice)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除无SN号设备", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "id", value = "无SN号设备ID", required = true, dataType = "Long", paramType = "path")
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity deleteDMcuDevice(@PathVariable Long id) {
        if (mIDMcuDeviceService.deleteById(id)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}
